import DataItemModel from "./DataItemModel";
import IXlsxTransform from "../../cfw/xlsx/IXlsxTransform";
import { TSMap } from "../../cfw/struct/TSMap";
import RowModel from "../../cfw/mvc/RowModel";
import { MapList } from "../../cfw/struct/MapList";

//model 容器 处理配置数据
export default class ModelManager<T extends RowModel> {

    protected itemMap: MapList<string | number, T> = new MapList()
    // protected itemList: T[] = []

    protected data: IXlsxTransform;

    protected indexData: TSMap<string, T[]> = new TSMap()

    getData(): IXlsxTransform {
        return this.data;
    }

    getNewModel(id: string | number, type: { new(param?: any): T; }, param?: any) {
        let ins = new type(param);
        let value = this.data.getRowData(id)
        ins.setCfg(value)
        return ins;
    }

    getNewModelByKey(id: any, type: { new(): T; }, key: string) {
        let model = this.getNewModel(id, type)
        model.setKey(key)
        return model;
    }
    initWithData(data: IXlsxTransform, type: { new(param?: any): T; }, isCreate: boolean = false, param?: any) {
        this.data = data;
        if (!this.data) {
            console.warn(' initVithData is null ');
            return;
        }
        if (isCreate) {
            let list = data.getList();
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                let ins = new type(param);
                ins.setCfg(element)
                this.itemMap.set(element.id, ins)
            }
            // data.forEach((key, value: any) => {

            // })
        }

    }

    getByIndex(index: number) {
        let itemList = this.itemMap.getList();
        if (index >= 0 && index <= itemList.length - 1) {
            return itemList[index]
        }
    }

    getByID(id: any, type: { new(param?: any): T; }) {
        if (this.itemMap.has(id)) {
            return this.itemMap.get(id)
        } else {
            return this.getNewModel(id, type)
        }
    }

    size() {
        return this.itemMap.length
    }


    getList() {
        return this.itemMap.getList();
    }

    getMap() {
        return this.itemMap;
    }


    clear() {
        this.data = null;
        this.itemMap.clear()
    }


    getIndexData(eValue: number, value: any, type: { new(param?: any): T; }) {
        if (!this.data) {
            console.warn(' getIndexData error ', eValue, value)
            return [];
        }
        let key = eValue + value;
        if (!this.indexData.has(key)) {
            let temp = []
            let list = this.data.getIndexByID(eValue, value)
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                temp.push(this.getByID(element, type))
            }
            this.indexData.set(key, temp)
        }

        return this.indexData.get(key)
    }

    sortitemList(func: (a: T, b: T) => number) {
        this.itemMap.getList().sort(func)
    }

    copy() {
        let list = []
        let itemList = this.itemMap.getList();
        for (let index = 0; index < itemList.length; index++) {
            const element = itemList[index];
            list.push(element)
        }
        return list;
    }
}